<?
session_start();
include 'php/connect.php';
include 'php/functions.php';


$specId = mysql_real_escape_string($_POST['specId']);

if(isset($_POST['specId'])){

	$output .= "<html>
					<head>
						<!-- Spec Detail Window -->
					</head>
					<body>
					<script type='text/javascript'>
						$(function(){
							$('.spec_avg').children().not(':radio').hide();
							$('.spec_avg').stars({
								disabled: true
							});
							$('.spec_rate').children().not(':radio').hide();
							$('.spec_rate').stars({
								cancelShow: false
								,callback: function(ui, type, value){
									$('#spec-comments').html(\"<div class='center'><img class='spec-comment-loader-image' src='media/loader.gif'></img></div>\");
									$.post('php/saveRating.php', {specId: ".$specId.", val: value},
										function(data){
											if(data.status == 'success'){
												loadSpecComments(".$specId.",5);
											}
										   }, 'json');
								
								}
							});
						});
						
						var mustLoad = true;
						$(document).ready(function(){
							$(function() {
								$('#show_spec_popup').draggable({cancel: '#spec_popup_body'});
							});
						});
						$('#spec-snapshot').click(function(e) {	
							showSpecPop(1);
						});
				</script>
				<div id='show_spec_popup'></div>";
	
$getRating = false;	
if(isset($_SESSION['userId'])){
	$uid = $_SESSION['userId'];
	$getRating = true;
}
					
						
	$abfrage = "SELECT spec.specId
		   ,spec.userId
		   ,spec.layout
		   ,spec.jobId
		   ,spec.level
		   ,job.tri as jobTri
		   ,job.name as jobName
		   ,spec.description
		   ,spec.public
		   ,spec.cdate
		   ,spec.udate
		   ";
		   
	if($getRating){
		$abfrage .= "
			,rate.value
			FROM ((
			ffxivc_specs as spec 
			LEFT JOIN 
			ffxivc_jobs as job 
			ON job.jobId = spec.jobId) 
			LEFT JOIN ffxivc_ratings as rate
			ON spec.specId = rate.specId
			AND rate.userId = '".$uid."'
			)
			WHERE spec.specId = \"".$specId."\"";
	}else{
		$abfrage .= "
			FROM (
			ffxivc_specs as spec 
			LEFT JOIN 
			ffxivc_jobs as job ON job.jobId = spec.jobId) 
			WHERE spec.specId = \"".$specId."\"";
	}
		  
	
	
	

	$res = mysql_query($abfrage);
	if ($res){
		$num = mysql_num_rows($res);
		if($num == 1){
		
			$getAverage = "SELECT 
					count(rateId) as count
					,sum(value) as value
					FROM ffxivc_ratings
					WHERE specId = \"".$specId."\"";
					
			$resAverage = mysql_query($getAverage);
			if ($resAverage){
				$count = mysql_result($resAverage, 0, "count");
				$value = mysql_result($resAverage, 0, "value");
				if($count != 0){
					$average = floor($value / $count);
				}else{
					$average = 0;
				}
			}
			
			
			
			$ud = mysql_result($res, 0, "udate");
			if($getRating){
				$rateValue = mysql_result($res, 0, "value");
			}
			$output .= "
						<div><img id='spec-snapshot' class='cur-point' src='media/specImages/".$specId.".png?".time()."'/></div>
						<script>
								function showSpecPop(toggle) {
									if(toggle=='1'){
										if(mustLoad){
											$('#show_spec_popup').load('php/showSpecImage.php', {sId: ".$specId."});
											mustLoad = false;
										}
										$('#show_spec_popup').css('top', (($(window).height() - $('#show_spec_popup').outerHeight()) / 2) + $(window).scrollTop() + 'px');
										$('#show_spec_popup').css('left', (($(window).width() - $('#show_spec_popup').outerWidth()) / 2) + $(window).scrollLeft() + 'px');
										$('#show_spec_popup').fadeIn('fast');
									}else{
										$('#show_spec_popup').fadeOut('fast');
									}
								};
						</script>
						<!--<div class='normal-text bold-text' style='width:310px;max-height: 40px;overflow: hidden;'>".$desc."</div>-->
						<div>
							<div class='fl-left'>
								<!--<div class='fl-left normal-text bold-text blue-text pad-10 font-10'>Average: </div>-->
								<div class='fl-right'>
									<div class='fl-left pad-t-10'>
										<form class='spec_avg' action='' method='post' style='padding-left: 10px;padding-top: 1px;'>";
										foreach (get_options() as $id => $title){
											$output .= 	"<input type='radio' name='rate' value='".$id."' id='avg".$id."'";
											if($id==$average){
												$output .= 	" checked";
											}
											$output .= 	" />";
										}
				$output .= 	"
										</form>
									</div>
									<div class='fl-right normal-text blue-text pad-10' style='padding-left:5px;'>( Rating: ".$average.", Votes: ".$count." )</div>
								</div>
							</div>";
			if($getRating){
			
				$output .= 	"
							<div class='fl-right' style='padding-right: 8px;'>
								<div class='fl-left normal-text blue-text pad-10'>Rate: </div>
								<div class='fl-right  pad-t-10'>
									<form class='spec_rate' action='' method='post' style='padding-top: 1px;'>";
										foreach (get_options() as $id => $title){
											$output .= 	"<input type='radio' name='rate' value='".$id."' id='rate".$id."'";
											if($id==$rateValue){
												$output .= 	" checked";
											}
											$output .= 	" />";
										}
				$output .= 	"
									</form>
								</div>
							</div>
						</div>";
			}
				$output .= 	"
					</div>
				</div>";	
			
		}
		
	}
					
	
					
	$output .= "	</body>
				</html>";
}

echo $output;


?>
